package leetcode.dynamic;

import java.util.Collections;
import java.util.HashSet;
import java.util.List;

public class Lc139 {


    public boolean wordBreak(String s, List<String> wordDict) {

        HashSet<String> wordSet = new HashSet<>(wordDict);

        //dp[i] 代表字符串s(0, i - 1)是否能够用wordDict的单词组成
        //dp[i] = dp[j] && check(s[j, i - 1]), check(str)判断str是否在字段中
        boolean[] dp = new boolean[s.length() + 1];
        dp[0] = true;
        for (int i = 1; i <= s.length(); i++) {
            for (int j = 0; j < i; j++) {
                if (dp[j] && wordSet.contains(s.substring(j, i))) {
                    dp[i] = true;
                    break;
                }
            }
        }
        return dp[s.length()];
    }




}
